home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PCGPEV10.ZIP / DMA_VLA.TXT < prev    next >
Text File  |  1994-05-10  |  14KB  |  507 lines

  1.  
  2. ─────────────────────────────────────────────────────────────────────────────
  3.                        INTRO TO DMA by Draeden of VLA
  4. ─────────────────────────────────────────────────────────────────────────────
  5.     
  6.  
  7.     DMA means Direct Memory Access.  You probably already know where and
  8. why you use it, so I'll skip right down to the dirty stuff.  This all 
  9. should speak for it's self, so... Enjoy.
  10.  
  11.     Draeden /VLA
  12.  
  13. ─────────────────────────────────────────────────────────────────────────────
  14.  
  15.     To do a DMA transfer, you need to know a few things:
  16.  
  17.         1)  Address of the memory to access
  18.  
  19.         2)  Length of data to read/write
  20.  
  21.     This can all be put into a structure:
  22.     
  23. STRUC DMAInfo
  24.     Page        db  ?
  25.     Offset      dw  ?
  26.     Length      dw  ?
  27. ENDS
  28.  
  29.     Page is the highest 4 bits of the absolute 20 bit address of the memory
  30. location.  Note that DMA transfers CANNOT cross 64k page boundries.
  31.     
  32.     The Length is actually LENGTH-1; sending in a 0 will move 1 byte,
  33. sending a 0FFFFh will move 64k.
  34.  
  35.     ─────────────────────────────────────────────────────────────────────
  36.     ; IN: DX:AX = segment/offset address of memory area
  37.     ;
  38.     ;OUT: DH = Page (0-F)  (DL is destroyed)
  39.     ;     AX = Offset
  40.     ─────────────────────────────────────────────────────────────────────
  41. PROC MakePage
  42.     push    bx
  43.  
  44.     mov     bl,dh
  45.     shr     bl,4    ;isolate upper 4 bits of segment
  46.  
  47.     shl     dx,4    ;make segment into ABS address
  48.     add     ax,dx   ;add the offset and put it in AX
  49.     adc     bl,0    ;complete the addition
  50.  
  51.     mov     dh,bl   ;put the PAGE where it goes
  52.  
  53.     pop     bx      ; DH:AX is now the PAGE:OFFSET address
  54.     ret
  55. ENDP
  56.  
  57. ─────────────────────────────────────────────────────────────────────────────
  58.     Programming DMA channels 0 thru 3
  59. ─────────────────────────────────────────────────────────────────────────────
  60.     There are 3 ports that are DMA channel specific:
  61.  
  62.         1) The Page register
  63.         2) The DMA count (length) register
  64.         3) The memory address (offset register)
  65.  
  66.     They are as follows:
  67.  
  68. DMACH   PAGE    ADDRESS  LENGTH
  69.  
  70.  0       87h       0       1
  71.  
  72.  1       83h       2       3
  73.  
  74.  2       81h       4       5
  75.  
  76.  3       82h       6       7
  77.  
  78.         
  79.     And now some general registers:
  80.  
  81.  DMA Mask Register:  0Ah
  82. ────────────────────────
  83.         bit 7 - 3 = 0  Reserved
  84.  
  85.             bit 2 = 0  clear mask
  86.                   = 1  set mask
  87.  
  88.        bits 1 - 0 = 00 Select channel 0
  89.                   = 01 select channel 1
  90.                   = 10 select channel 2
  91.                   = 11 select channel 3
  92.  
  93.        USE: You must set the mask of the channel before you
  94.             can reprogram it.
  95.  
  96.  DMA Mode Register:  0Bh
  97. ────────────────────────
  98.         bit 7 - 6 = 00 Demand mode
  99.                   = 01 Signal mode
  100.                   = 10 Block mode
  101.                   = 11 Cascade mode
  102.  
  103.         bit 5 - 4 = 0  Reserved
  104.  
  105.         bit 3 - 2 = 00 Verify operation
  106.                   = 01 Write operation
  107.                   = 10 Read operation
  108.                   = 11 Reserved
  109.  
  110.        bits 1 - 0 = 00 Select channel 0
  111.                   = 01 select channel 1
  112.                   = 10 select channel 2
  113.                   = 11 select channel 3
  114.  
  115.        USE: Tell the DMAC what to do. Common modes are:
  116.  
  117.             48h (Read operation, Signal mode)
  118.                 Used to read data from host memory and send to whomever
  119.                 polls it.
  120.  
  121.             44h (Write operation, Signal mode)
  122.                 Used to write data taken from a device to memory.
  123.  
  124. DMA clear byte ptr:  0Ch
  125. ────────────────────────
  126.        USE: Send a zero to reset the internal ptrs
  127.  
  128.  
  129.  
  130.     WHAT TO DO:
  131. ────────────────────────
  132.  
  133.     1) Set the Mask bit for the channel
  134.  
  135.         mov     al,4
  136.         add     al,[DMA_Channel]
  137.         out     0ah,al
  138.  
  139.     2) Clear Byte Ptr
  140.  
  141.         sub     al,al
  142.         out     0Ch,al
  143.  
  144.     3) Set the DMA transfer mode
  145.  
  146.         mov     al,48h                  ;MODE output (read)
  147.         add     al,[DMA_Channel]
  148.         out     0Bh,al
  149.  
  150.     4) Set the memory ADDRESS and LENGTH
  151.  
  152.         ;        AX = offset
  153.         ;        CX = Length
  154.         ;[DMA_Base] = port # of memory address
  155.         
  156.         mov     dx,[DMA_Base]
  157.         out     dx,al                   ;send lower byte address
  158.         mov     al,ah
  159.         out     dx,al                   ;send high byte address
  160.  
  161.         inc     dl                  ;point to Count port
  162.         mov     al,cl
  163.         out     dx,al                   ;send low byte length
  164.         mov     al,ch
  165.         out     dx,al                   ;send high byte length
  166.  
  167.     5) Set the DMA page
  168.  
  169.         ; AL = Page
  170.  
  171.         mov     dx,[Dma_Page]
  172.         out     dx,al                   ; write the Page
  173.  
  174.     6) Clear DMA mask bit
  175.  
  176.         mov     al,[byte DMA_Channel]
  177.         out     0Ah,al                  ; port 0Ah, DMA-1 mask reg bit
  178.  
  179.     7) Program the other device that is going to use the DMA output/input
  180.  
  181.  
  182.     ─────────────────────────────────────────────────────────────────────
  183.     ; This routine programs the DMAC for channels 0-3
  184.     ;
  185.     ; IN: [DMA_Channel], [DMAbaseAdd], [DMApageReg] must be setup
  186.     ;       [DAMBaseAdd] =  Memory Address port
  187.     ;
  188.     ;     dh = mode
  189.     ;     ax = address
  190.     ;     cx = length  
  191.     ;     dl = page
  192.     ─────────────────────────────────────────────────────────────────────
  193. PROC Prog_DMA03 NEAR
  194.         push    bx
  195.         mov     bx,ax
  196.  
  197.         mov     al,4
  198.         add     al,[DMA_Channel]
  199.         out     0Ah,al          ; mask reg bit
  200.  
  201.         sub     al,al
  202.         out     0Ch,al          ; clr byte ptr
  203.  
  204.         mov     al,dh
  205.         add     al,[DMA_Channel]
  206.         out     0Bh,al          ; set mode reg
  207.  
  208.         push    dx
  209.         
  210.         mov     dx,[DMAbaseAdd]
  211.         mov     al,bl
  212.         out     dx,al           ; set base address low
  213.         mov     al,bh
  214.         out     dx,al           ; set base address high
  215.  
  216.         inc     dx              ;point to length
  217.         mov     al,cl
  218.         out     dx,al           ; set length low
  219.         mov     al,ch
  220.         out     dx,al           ; set length high
  221.  
  222.         pop     dx
  223.  
  224.         mov     al,dl
  225.         mov     dx,[DmaPageReg]
  226.         out     dx,al           ; set DMA page reg
  227.  
  228.         mov     al,[DMA_Channel]
  229.         out     0Ah,al          ; unmask (activate) dma channel
  230.         pop     bx
  231.         ret
  232. ENDP
  233.  
  234. ─────────────────────────────────────────────────────────────────────────────
  235. ─────────────────────────────────────────────────────────────────────────────
  236.     Programming DMA channels 4 thru 7
  237. ─────────────────────────────────────────────────────────────────────────────
  238. ─────────────────────────────────────────────────────────────────────────────
  239.  
  240.     Again, there are 3 ports that are DMA channel specific:
  241.  
  242.         1) The Page register
  243.         2) The DMA count (length) register
  244.         3) The memory address (offset register
  245.  
  246.     They are as follows:
  247.  
  248. DMACH   PAGE    ADDRESS  LENGTH
  249.  
  250.  4       8Fh      C0h      C2h  
  251.  
  252.  5       8Bh      C4h      C6h  
  253.  
  254.  6       89h      C8h      CAh  
  255.  
  256.  7       8Ah      CCh      CEh 
  257.  
  258.  
  259.     And now some general registers:
  260.  
  261.  DMA Mask Register: 0D4h
  262. ────────────────────────
  263.         bit 7 - 3 = 0  Reserved
  264.  
  265.             bit 2 = 0  clear mask
  266.                   = 1  set mask
  267.  
  268.        bits 1 - 0 = 00 Select channel 4
  269.                   = 01 select channel 5
  270.                   = 10 select channel 6
  271.                   = 11 select channel 7
  272.  
  273.        USE: You must set the mask of the channel before you
  274.             can reprogram it.
  275.  
  276.  DMA Mode Register: 0D6h
  277. ────────────────────────
  278.         bit 7 - 6 = 00 Demand mode
  279.                   = 01 Signal mode
  280.                   = 10 Block mode
  281.                   = 11 Cascade mode
  282.  
  283.         bit 5 - 4 = 0  Reserved
  284.  
  285.         bit 3 - 2 = 00 Verify operation
  286.                   = 01 Write operation
  287.                   = 10 Read operation
  288.                   = 11 Reserved
  289.  
  290.        bits 1 - 0 = 00 Select channel 4
  291.                   = 01 select channel 5
  292.                   = 10 select channel 6
  293.                   = 11 select channel 7
  294.  
  295.        USE: Tell the DMAC what to do. Common modes are:
  296.  
  297.             48h (Read operation, Signal mode)
  298.                 Used to read data from host memory and send to whomever
  299.                 polls it.
  300.  
  301.             44h (Write operation, Signal mode)
  302.                 Used to write data taken from a device to memory.
  303.  
  304. DMA clear byte ptr: 0D8h
  305. ────────────────────────
  306.        USE: Send a zero to reset the internal ptrs
  307.  
  308.  
  309.     WHAT TO DO: (exactly the same thing, just different io PORTs)
  310. ────────────────────────
  311.  
  312.     1) Set the Mask bit for the channel
  313.  
  314.         mov     al,[DMA_Channel]    ;because the DMA's are 4-7, bit #3
  315.         out     0D4h,al             ; is already set
  316.  
  317.     2) Clear Byte Ptr
  318.  
  319.         sub     al,al
  320.         out     0D8h,al
  321.  
  322.     3) Set the DMA transfer mode
  323.         
  324.         mov     al,[DMA_Channel]
  325.         sub     al,4
  326.         or      al,48h                  ;MODE output (read)
  327.         out     0D6h,al
  328.  
  329.     4) Set the memory ADDRESS and LENGTH
  330.  
  331.         ;        AX = offset
  332.         ;        CX = Length
  333.         ;[DMA_Base] = port # of memory address
  334.         
  335.         mov     dx,[DMA_Base]
  336.         out     dx,al                   ;send lower byte address
  337.         mov     al,ah
  338.         out     dx,al                   ;send high byte address
  339.  
  340.         add     dl,2                ;point to Count port (seperated by 2)
  341.         mov     al,cl
  342.         out     dx,al                   ;send low byte length
  343.         mov     al,ch
  344.         out     dx,al                   ;send high byte length
  345.  
  346.     5) Set the DMA page
  347.  
  348.         ; AL = Page
  349.  
  350.         mov     dx,[Dma_Page]
  351.         out     dx,al                   ; write the Page
  352.  
  353.     6) Clear DMA mask bit
  354.  
  355.         mov     al,[byte DMA_Channel]
  356.         and     al,00000011b
  357.         out     0d4h,al                 ; port 0Ah, DMA-1 mask reg bit
  358.  
  359.     7) Program the other device that is going to use the DMA output/input
  360.  
  361.  
  362.     ─────────────────────────────────────────────────────────────────────
  363.     ; This routine programs the DMAC for channels 4-7
  364.     ;
  365.     ; IN: [DMA_Channel], [DMAbaseAdd], [DMApageReg] must be setup
  366.     ;       [DAMBaseAdd] =  Memory Address port
  367.     ;
  368.     ;     dh = mode
  369.     ;     ax = address
  370.     ;     cx = length  
  371.     ;     dl = page
  372.     ─────────────────────────────────────────────────────────────────────
  373. PROC Prog_DMA47 NEAR
  374.         push    bx
  375.         mov     bx,ax
  376.  
  377.         mov     al,[DMA_Channel]
  378.         out     0D4h,al         ; mask reg bit
  379.  
  380.         sub     al,al
  381.         out     0D8h,al         ; clr byte ptr
  382.  
  383.         mov     al,[DMA_Channel]
  384.         sub     al,4
  385.         add     al,dh
  386.         out     0D6h,al         ; set mode reg
  387.  
  388.         push    dx
  389.  
  390.         mov     dx,[DMAbaseAdd]
  391.         mov     al,bl
  392.         out     dx,al           ; set base address low
  393.         mov     al,bh
  394.         out     dx,al           ; set base address high
  395.  
  396.         add     dl,2            ;point to length
  397.         mov     al,cl
  398.         out     dx,al           ; set length low
  399.         mov     al,ch
  400.         out     dx,al           ; set length high
  401.  
  402.         pop     dx
  403.  
  404.         mov     al,dl
  405.         mov     dx,[DmaPageReg]
  406.         out     dx,al           ; set DMA page reg
  407.  
  408.         mov     al,[DMA_Channel]
  409.         and     al,00000011b
  410.         out     0D4h,al         ; unmask (activate) dma channel
  411.         pop     bx
  412.         ret
  413. ENDP
  414.  
  415.     ─────────────────────────────────────────────────────────────────────
  416.     ; This routine programs the DMAC for channels 0-7
  417.     ;
  418.     ; IN: [DMA_Channel], [DMAbaseAdd], [DMApageReg] must be setup
  419.     ;       [DAMBaseAdd] =  Memory Address port
  420.     ;
  421.     ;     dh = mode
  422.     ;     ax = address
  423.     ;     cx = length  
  424.     ;     dl = page
  425.     ─────────────────────────────────────────────────────────────────────
  426. PROC Prog_DMA NEAR
  427.         push    bx
  428.         mov     bx,ax
  429.  
  430.         cmp     [DMA_Channel],4
  431.         jb      @@DoDMA03
  432.  
  433.         mov     al,[DMA_Channel]
  434.         out     0D4h,al         ; mask reg bit
  435.  
  436.         sub     al,al
  437.         out     0D8h,al         ; clr byte ptr
  438.  
  439.         mov     al,[DMA_Channel]
  440.         sub     al,4
  441.         add     al,dh
  442.         out     0D6h,al         ; set mode reg
  443.  
  444.         push    dx
  445.  
  446.         mov     dx,[DMAbaseAdd]
  447.         mov     al,bl
  448.         out     dx,al           ; set base address low
  449.         mov     al,bh
  450.         out     dx,al           ; set base address high
  451.  
  452.         add     dl,2            ;point to length
  453.         mov     al,cl
  454.         out     dx,al           ; set length low
  455.         mov     al,ch
  456.         out     dx,al           ; set length high
  457.  
  458.         pop     dx
  459.  
  460.         mov     al,dl
  461.         mov     dx,[DmaPageReg]
  462.         out     dx,al           ; set DMA page reg
  463.  
  464.         mov     al,[DMA_Channel]
  465.         and     al,00000011b
  466.         out     0D4h,al         ; unmask (activate) dma channel
  467.         pop     bx
  468.         ret
  469.  
  470. @@DoDMA03:
  471.         mov     al,4
  472.         add     al,[DMA_Channel]
  473.         out     0Ah,al          ; mask reg bit
  474.  
  475.         sub     al,al
  476.         out     0Ch,al          ; clr byte ptr
  477.  
  478.         mov     al,dh
  479.         add     al,[DMA_Channel]
  480.         out     0Bh,al          ; set mode reg
  481.  
  482.         push    dx
  483.         
  484.         mov     dx,[DMAbaseAdd]
  485.         mov     al,bl
  486.         out     dx,al           ; set base address low
  487.         mov     al,bh
  488.         out     dx,al           ; set base address high
  489.  
  490.         inc     dx              ;point to length
  491.         mov     al,cl
  492.         out     dx,al           ; set length low
  493.         mov     al,ch
  494.         out     dx,al           ; set length high
  495.  
  496.         pop     dx
  497.  
  498.         mov     al,dl
  499.         mov     dx,[DmaPageReg]
  500.         out     dx,al           ; set DMA page reg
  501.  
  502.         mov     al,[DMA_Channel]
  503.         out     0Ah,al          ; unmask (activate) dma channel
  504.         pop     bx
  505.         ret
  506. ENDP
  507.